<template>
  <div
    class="wrap-item"
    :style="{
      '--scroll-width': '175px',
    }"
  >
    <!-- 搜索-->
    <el-form ref="formSearch" :model="formSearch" size="small" inline>
      <el-form-item>
        <el-input
          v-model.trim="formSearch.param.companyName"
          placeholder="请输入企业名称"
          maxlength="30"
          clearable
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-select
          clearable
          v-model="formSearch.param.companyType"
          placeholder="请选择企业类型"
        >
          <el-option
            v-for="item in companyType"
            :key="item.dicValue"
            :label="item.dicLabel"
            :value="item.dicValue"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-select
          clearable
          v-model="formSearch.param.userItemType"
          placeholder="请选择入驻类型"
        >
          <el-option
            v-for="item in userItemType"
            :key="item.dicValue"
            :label="item.dicLabel"
            :value="item.dicValue"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-select
          clearable
          v-model="formSearch.param.status"
          placeholder="请选择状态"
        >
          <el-option label="正常" value="0"></el-option>
          <el-option label="拉黑" value="10"></el-option>
          <el-option label="注销" value="20"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-date-picker
          v-model="formSearch.param.date"
          @change="dateChange"
          type="daterange"
          range-separator="至"
          value-format="yyyy-MM-dd"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          style="width: 100%"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <span class="wrap-btn wrap-primary" @click="search">查询</span>
        <span
          class="wrap-btn wrap-warning"
          @click="download"
          v-if="$checkPermission(108)"
          >导出</span
        >
      </el-form-item>
    </el-form>
    <!-- 数据列表 -->
    <div class="wrap-other-roll">
      <el-table
        :data="tableData"
        :header-cell-style="{ background: '#F6F7F9' }"
      >
        <el-table-column
          type="index"
          width="50"
          label="序号"
          align="center"
        ></el-table-column>
        <el-table-column prop="companyName" label="企业名称"></el-table-column>
        <el-table-column prop="companyTypeTxt" label="企业类型">
        </el-table-column>
        <el-table-column prop="userItemTypeTxt" label="入住类型">
        </el-table-column>
        <el-table-column prop="contactName" label="联系人"></el-table-column>
        <el-table-column prop="contactTel" label="联系电话"></el-table-column>
        <el-table-column
          prop="addTime"
          label="入驻申请时间"
          width="160px"
        ></el-table-column>
        <el-table-column label="状态" width="80px" align="center">
          <template slot-scope="scope">
            <el-link
              type="success"
              :underline="false"
              v-if="scope.row.companyStatus == 0"
              >正常</el-link
            >
            <el-link
              type="danger"
              :underline="false"
              v-if="scope.row.companyStatus == 10"
              >拉黑
            </el-link>
            <el-link
              type="danger"
              :underline="false"
              v-if="scope.row.companyStatus == 20"
              >注销
            </el-link>
          </template>
        </el-table-column>
        <el-table-column
          prop="content"
          label="操作"
          width="80px"
          align="center"
        >
          <template slot-scope="scope">
            <el-dropdown @command="(command) => handle(command, scope.row)">
              <span class="el-dropdown-link">
                更多
                <i class="el-icon-arrow-down el-icon--right"></i>
              </span>
              <el-dropdown-menu slot="dropdown">
                <el-dropdown-item command="view" v-if="$checkPermission(104)"
                  >查看</el-dropdown-item
                >
                <el-dropdown-item command="pull" v-if="$checkPermission(122)"
                  >拉黑</el-dropdown-item
                >
              </el-dropdown-menu>
            </el-dropdown>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 分页 -->
    <web-page
      :page="formSearch.pageNum"
      :size="formSearch.pageSize"
      :total="total"
      @sizeChange="sizeChange"
      @currentChange="currentChange"
    ></web-page>
    <!-- 拉黑 -->
    <el-dialog title="备注" :visible.sync="visible" width="888px">
      <el-form label-width="80px" :model="form" size="medium">
        <el-form-item label="备注" required>
          <el-input
            type="textarea"
            v-model="form.remark"
            maxlength="100"
            placeholder="请填写备注"
            rows="2"
            resize="none"
          ></el-input>
        </el-form-item>
        <el-form-item style="text-align: end">
          <span class="wrap-btn wrap-success" @click="save">确定</span>
          <span class="wrap-btn wrap-default" @click="visible = false"
            >取消</span
          >
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      companyType: [],
      userItemType: [],
      formSearch: {
        pageNum: 1,
        pageSize: 10,
        param: {},
      },
      total: null,
      tableData: [],
      form: {},
      visible: false,
    }
  },
  created() {
    this.listDic('userItemType') //入驻身份
    this.listDic('companyType') //企业类型
    this.itemList()
  },
  methods: {
    itemList() {
      // 获取列表
      this.$axios.post('/admin/company/page', this.formSearch).then((data) => {
        this.tableData = []
        if (data.code == 200) {
          this.tableData = data.data.list
          this.total = data.data.total
        } else {
          this.$message.error(data.msg)
        }
      })
    },
    sizeChange(val) {
      //每页条数
      this.formSearch.pageSize = val
      this.itemList()
    },
    currentChange(val) {
      //分页
      this.formSearch.pageNum = val
      this.itemList()
    },
    search() {
      //搜索
      this.formSearch.pageNum = 1
      this.itemList()
    },
    dateChange(val) {
      // 时间范围发生变化
      if (val) {
        this.formSearch.param.start = val[0]
        this.formSearch.param.stop = val[1]
      } else {
        this.formSearch.param.start = ''
        this.formSearch.param.stop = ''
      }
    },
    handle(command, row) {
      //操作
      if (!command) {
        this.$message.error('菜单选项缺少command属性')
        return
      }
      switch (command) {
        case 'view':
          //查看
          this.$router.push({
            path: '/supplierCenter/manage/detail',
            query: { id: row.id },
          })
          break
        case 'pull':
          //拉黑
          this.form.id = row.id
          this.visible = true
          break
      }
    },
    save() {
      //保存
      if (!this.form.remark) {
        this.$message.error('请填写备注')
        return false
      }
      this.$axios
        .get(
          `/admin/company/black?id=${this.form.id}&remark=${this.form.remark}`
        )
        .then((data) => {
          if (data.code == 200) {
            this.itemList()
            this.visible = false
            this.$message.success('操作成功！')
          } else {
            this.$message.error(data.msg)
          }
        })
    },
    download() {
      //导出
      this.$axios
        .post('/admin/company/exportExcel', this.formSearch, {
          responseType: 'blob',
        })
        .then((data) => {
          const reader = new FileReader()
          reader.readAsDataURL(data)
          reader.onload = function (e) {
            const a = document.createElement('a')
            a.download = '供应商'
            a.href = e.target.result
            a.click()
          }
        })
    },
    listDic(item) {
      // 获取字典
      this.$axios.get(`/admin/dic/listDic?type=${item}`).then((data) => {
        this[item] = []
        if (data.code == 200) {
          this[item] = data.data
        } else {
          this.$message.error(data.msg)
        }
      })
    },
  },
}
</script>
